﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Ajax_Draw : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    { 
        HttpContext c = HttpContext.Current;
        string rtn = "fail";
        Model.CMember modelcm = BLL.CMember.UserLoginInfo();
        Model.UsersAccount uaccout = null; 
        int type = ReadRequest.RequestInt("type");
        if (modelcm != null)
        {
            Model.DrawNumber drawCout = BLL.DrawNumber.NewsTopOne("select * FROM DrawNumber where DateDiff(dd,CurTime,GETDATE())=0  and  UserId=" + modelcm.Id);
            //判断抽奖次数是否为三次
            if (drawCout == null)
            {
                Model.DrawNumber CurdrawCout = new Model.DrawNumber();
                CurdrawCout.UserId = modelcm.Id;
                CurdrawCout.Count = 3;
                CurdrawCout.CurTime = DateTime.Now;
                BLL.DrawNumber.Add(CurdrawCout);
                rtn = "sucess";
            }
            else
            {
                drawCout = BLL.DrawNumber.NewsTopOne("select * FROM DrawNumber where DateDiff(dd,CurTime,GETDATE())=0  and  UserId=" + modelcm.Id);
                uaccout = BLL.UsersAccount.GetModeluser_id(modelcm.Id);
                if (drawCout.Count > 0)
                {
                    if (uaccout.integralTotal >= 10)
                    {
                        rtn = "sucess";
                        //扣除次数
                        drawCout.Count = drawCout.Count - 1;
                        BLL.DrawNumber.Update(drawCout);
                        //扣除积分
                        uaccout.integralTotal = uaccout.integralTotal - 10;
                        BLL.UsersAccount.Update(uaccout);

                        this.InsertIngratel(modelcm.Id,2,"抽奖一次扣除积分10分",10);

                        if (type == 0)
                        {
                            //1等奖
                        }
                        else if (type == 2)
                        {
                            //6等奖
                        }
                        else if (type == 4)
                        {
                            //5等奖
                        }
                        else if (type == 5)
                        {
                            //4等奖
                        }
                        else if (type == 7)
                        {
                            //3等奖
                        }
                        else if (type == 8)
                        {
                            //2等奖
                        }

                    }
                    else
                    {
                        rtn = "您的积分不足，不能抽奖！";
                    }
                }
                else
                {
                    rtn = "您的抽奖次数已经用完,此次抽奖无效！";
                }
            }
        }
        else
        {
            rtn = "请重新登陆！";
        }

        Response.Write(rtn);
        Response.End();
    }
    
    /// <summary>
    /// 添加积分获得使用信息表
    /// </summary>
    /// <param name="userid">用户id</param>
    /// <param name="type">1.获得，2消费</param>
    /// <param name="content">使用说明</param>
    /// <param name="userIntegral">使用积分数</param>
    private void InsertIngratel(long userid,int type,string content,decimal userIntegral)
    {
        Model.IntegralExchange Ingratel = new Model.IntegralExchange();
        Ingratel.Type = type;
        Ingratel.UseIntegral = userIntegral;
        Ingratel.OrderId = DateTime.Now.ToString("yyyyMMddhhmmss");
        Ingratel.Addtime = DateTime.Now;
        Ingratel.Content = content;
        Ingratel.IsSuccess = true;
        Ingratel.UserId = userid;
        BLL.IntegralExchange.Add(Ingratel);
    }
    /// <summary>
    /// 添加抽奖中奖表
    /// </summary>
    /// <param name="userid">用户id</param>
    /// <param name="nickname">用户名</param>
    /// <param name="title">中奖的标题</param>
    private void InsertDrawWin(long userid, string nickname, string title)
    {
        Model.DrawWin draw = new Model.DrawWin();
        draw.UserId = userid;
        draw.NickName = nickname;
        draw.DrawTitle = title;
        draw.Addtime = DateTime.Now;
        draw.IsSuccess = true;
        BLL.DrawWin.Add(draw);
    }
    /// <summary>
    /// 添加红包获得使用信息表
    /// </summary>
    /// <param name="userid">用户id</param>
    /// <param name="type">1.获得，2消费</param>
    /// <param name="content">使用说明</param>
    /// <param name="GoldCount">获得的金币数</param>
    private void InsertGold(long userid, int type, string content, decimal GoldCount)
    {
        Model.GoldExchange Ingratel = new Model.GoldExchange();
        Ingratel.Type = type;
        Ingratel.GoldCount = GoldCount;
        Ingratel.Addtime = DateTime.Now;
        Ingratel.Content = content;
        Ingratel.IsSuccess = true;
        Ingratel.UserId = userid;
        BLL.GoldExchange.Add(Ingratel);
    }
}